
function Banner(){
    this.timer = null;
    this.bannerGroup = $("#banner-group");
    this.bannerUl = $("#banner-ul");
    this.bannerCount = this.bannerUl.children().length;
    this.leftArrow = $(".left-arrow");
    this.rightArrow = $(".right-arrow");
    //控制轮播图的下标
    this.index = 1;
    //轮播图的宽度
    this.bannerWidth = 798;
    //小圆点的容器
    this.pageControl = $(".page-control");
}

/**
 * 显示或者隐藏箭头
 */
Banner.prototype.toggleArrow = function(){
    this.leftArrow.toggle();
    this.rightArrow.toggle();
};

/**
 * 监听左右箭头点击行为的控制行为
 */
Banner.prototype.listenArrowClick = function(){
    var self = this;
	var prev = $('.left-arrow');
	var next = $('.right-arrow');
	prev.click(function () {
		if (self.index <= 0) {
			self.bannerUl.css({'left': -self.bannerWidth*self.bannerCount})
			self.index = self.bannerCount-1;
		} else {
			self.index--;
		}
		self.animate();
	});
	next.click(function () {
		if (self.index >= self.bannerCount+1) {
			self.bannerUl.css({'left': -self.bannerWidth});
			self.index = 2;
		} else {
			self.index++;
		}
		self.animate();
	});
};

/**
 * 监听鼠标移动上轮播图的控制行为
 */
Banner.prototype.listenBannerHover = function(){
    var self = this;
    this.bannerGroup.hover(
        //把鼠标放到该对象上执行
        function(){
            clearInterval(self.timer);
            self.toggleArrow();
        },
        //当鼠标离开到该对象执行
        function(){
            self.bannerAction();
            self.toggleArrow();
        }
    );
};

/**
 * 控制轮播图根据下标动作一次
 */
Banner.prototype.animate = function(){
    this.bannerUl.stop().animate({left: -this.bannerWidth*this.index}, 500);
	var index = this.index;
	if (index === 0) {
		index = this.bannerCount-1;
	}else if(index === this.bannerCount+1) {
		index = 0;
	}else {
		index = this.index-1;
	}
	this.allLis.eq(index).addClass('current').siblings().removeClass('current');
};

/**
 * 循环播放轮播图
 */
Banner.prototype.bannerAction = function(){
    var self = this;
	this.timer = setInterval(function () {
		if (self.index >= self.bannerCount+1) {
			self.index = 2;
			self.bannerUl.css({'left': -self.bannerWidth})
		} else {
			self.index++;
		}
		self.animate();
	}, 2000);
};

/**
 * 初始化轮播图
 */
Banner.prototype.initBanner = function () {
	var first = this.bannerUl.children().eq(0).clone();
	var end = this.bannerUl.children().eq(this.bannerCount-1).clone();
	this.bannerUl.append(first);
	this.bannerUl.prepend(end);
	this.bannerUl.css({
		'width': (this.bannerCount+2)*this.bannerWidth,
		'left': -this.bannerWidth
	});
};

/**
 * 初始化小圆点
 */
Banner.prototype.initPageControl = function(){
    for (var i = 0; i < this.bannerCount; i++){
        var newLi = $("<li></li>");
        if (i === 0){
            newLi.addClass("current");
        }
        this.pageControl.append(newLi);
    }
};

/**
 * 给每个控制的小圆点加上对应的点击事件
 */
Banner.prototype.listenPageControlClick = function () {
    var self = this;
    this.allLis = $('#page-control').children();
    this.allLis.each(function (index, obj) {
        $(obj).click(function () {
            self.index = index + 1;
            self.animate();
        });
    })
};

/**
 * 入口
 */
Banner.prototype.run = function(){
    clearInterval(this.timer);
    this.initBanner();
    this.initPageControl();
    this.listenPageControlClick();
    this.bannerAction();
    this.listenArrowClick();
    this.listenBannerHover();
};

function Index(){
    this.page = 2;
    this.category_id = 0;
    this.loadMoreBtn = $('#load-more-btn');
}

/**
 * 点击加载更多使用arttemplte生成li标签拼接在后面
 */
Index.prototype.listenLoadMoreEvent = function(){
    var self = this;
    this.loadMoreBtn.click(function(){
        myajax.get({
            url: '/article/article_list/',
            data: {
                p: self.page,
                category_id: self.category_id
            },
            success: function(result){
                if (result['code'] === 200){
                    var articles = result['data'];
                    if (articles.length > 0){
                        var tpl = template('article-item', {articles});
                        $('.list-inner-group').append(tpl);
                        self.page++;
                    }else{
                        self.loadMoreBtn.hide();
                    }
                }
            }
        })
    });
};

/**
 * 点击分类切换事件
 */
Index.prototype.listenCategorySwitchEvent = function(){
    var tabGroup = $('.list-tag');
    var self = this;
    tabGroup.children().click(function(){
        var li = $(this);
        var category_id = li.attr('data-category');
        var page = 1;
        myajax.get({
            url: '/article/article_list/',
            data: {
                category_id,
                p: page
            },
            success: function(result){
                if (result['code'] === 200){
                    var articles = result['data'];
                    var lis = $('.list-inner-group');
                    lis.empty();
                    if (articles.length > 0){
                        self.loadMoreBtn.show();
                        var tpl = template('article-item', {articles});
                        lis.append(tpl);
                    }else{
                        lis.append($('<li>该分类下暂未有内容</li>'));
                        self.loadMoreBtn.hide();
                    }
                    self.page = 2;
                    self.category_id = category_id;
                    li.addClass('active').siblings().removeClass('active');
                }
            }
        });
    });
};

Index.prototype.run = function(){
    this.listenLoadMoreEvent();
    this.listenCategorySwitchEvent();
};

$(function(){
   var banner = new Banner();
   banner.run();

   var index = new Index();
   index.run();
});
